<?php
class Data_Model_KhoaHocMapper extends Data_Model_DataMapperAbstract{
	
	private static $_instance = null;
	private  $_paginator = null;
	protected $_ids;
	
	public function searchByMaKH($id)
	{
		$db = $this->getDb();
		$select = $db->select()
					->from('tbl_khoahoc')
					->join('tbl_dangkykhoahoc','tbl_dangkykhoahoc.MaKhoaHoc = tbl_khoahoc.MaKhoaHoc')
					->where('tbl_khoahoc.MaKhoaHoc = ?', $id);
		$khoahocs = $db->fetchAll($select);
		$khoahocArray = array();
		foreach($khoahocs as $khoahoc)
		{
			$giaovienMapper = Data_Model_GiaoVienMapper::getInstance();
			$giaovien = $giaovienMapper->searchById($khoahoc['MaGiaoVien']);
			$khoahoc['tbl_giaovien'] = $giaovien;
				
			$lophocMapper = Data_Model_LopHocMapper::getInstance();
			$lophoc = $lophocMapper->searchById($khoahoc['MaLop']);
			$khoahoc['tbl_lophoc'] = $lophoc;
	
			$khoahocArray[] = $this->_populate($khoahoc);
		}
		return $khoahocArray;
	}
	
	public function searchByMakhoahoc($id)
	{
		$db = $this->getDb();
		$select = $db->select()
					->from('tbl_khoahoc')
					->join('tbl_chitietkhoahoc','tbl_chitietkhoahoc.MaKhoaHoc = tbl_khoahoc.MaKhoaHoc')
					->where('tbl_khoahoc.MaKhoaHoc = ?', $id);
		$khoahocs = $db->fetchAll($select);
		$khoahocArray = array();
		foreach($khoahocs as $khoahoc)
		{
		$giaovienMapper = Data_Model_GiaoVienMapper::getInstance();
		$giaovien = $giaovienMapper->searchById($khoahoc['MaGiaoVien']);
		$khoahoc['tbl_giaovien'] = $giaovien;

		$kythiMapper = Data_Model_KyThiMapper::getInstance();
		$kythi = $kythiMapper->searchByMakhoahoc($id);
		$khoahoc['tbl_kythi'] = $kythi;
		
		$lophocMapper = Data_Model_LopHocMapper::getInstance();
		$lophoc = $lophocMapper->searchById($khoahoc['MaLop']);
		$khoahoc['tbl_lophoc'] = $lophoc;
		
		$khoahocArray[] = $this->_populate($khoahoc);
		}
		return $khoahocArray;
	}
	public function searchById($id)
	{
		$db = $this->getDb();
		$select = $db->select()
						->from('tbl_khoahoc')->where('MaKhoaHoc = ?', $id);
		$khoahoc = $db->fetchRow($select);
		
		$ctkhMapper = Data_Model_ChiTietKhoaHocMapper::getInstance();
		$ctkh = $ctkhMapper->searchByKh($id);
		$khoahoc['tbl_chitietkhoahoc'] = $ctkh;
		
		$giaovienMapper = Data_Model_GiaoVienMapper::getInstance();
		$giaovien = $giaovienMapper->searchById($khoahoc['MaGiaoVien']);
		$khoahoc['tbl_giaovien'] = $giaovien;
		
		$kythiMapper = Data_Model_KyThiMapper::getInstance();
		$kythi = $kythiMapper->searchByMakhoahoc($id);
		$khoahoc['tbl_kythi'] = $kythi;
			
		$lophocMapper = Data_Model_LopHocMapper::getInstance();
		$lophoc = $lophocMapper->searchById($khoahoc['MaLop']);
		$khoahoc['tbl_lophoc'] = $lophoc;
		
		return $this->_populate($khoahoc);
	}
	
	public function khoahocList()
	{
		$db = $this->getDb();
		$selectKhoahoc = $db->select()
							->from('tbl_khoahoc');
		$khoahocs = $db->fetchAll($selectKhoahoc);
	
		$khoahocsObjArray = array();
		
		foreach($khoahocs as $khoahoc)
		{
			$ctkhMapper = Data_Model_ChiTietKhoaHocMapper::getInstance();
			$ctkh = $ctkhMapper->searchByKh($khoahoc['MaKhoaHoc']);
			$khoahoc['tbl_chitietkhoahoc'] = $ctkh;
			
			$giaovienMapper = Data_Model_GiaoVienMapper::getInstance();
			$giaovien = $giaovienMapper->searchById($khoahoc['MaGiaoVien']);
			$khoahoc['tbl_giaovien'] = $giaovien;
			
			$kythiMapper = Data_Model_KyThiMapper::getInstance();
			$kythi = $kythiMapper->searchByMakhoahoc($khoahoc['MaKhoaHoc']);
			$khoahoc['tbl_kythi'] = $kythi;
			
			$lophocMapper = Data_Model_LopHocMapper::getInstance();
			$lophoc = $lophocMapper->searchById($khoahoc['MaLop']);
			$khoahoc['tbl_lophoc'] = $lophoc;
			
			$khoahocsObjArray[] = $this->_populate($khoahoc);
		}
		return $khoahocsObjArray;
	}
	protected function _populate($data)
	{
		$obj = new Data_Model_KhoaHoc();
		$obj->setId($data['MaKhoaHoc']);
		$obj->setMakhoahoc($data['TenMaKhoaHoc']);
		$obj->setTenkhoahoc($data['TenKhoaHoc']);
		$obj->setMalop($data['MaLop']);
		$obj->setNgaykhaigiang($data['NgayKhaiGiang']);
		$obj->setMagiaovien($data['MaGiaoVien']);
		$obj->setTrangthai($data['TrangThai']);
		$obj->setChitietkhoahoc($data['tbl_chitietkhoahoc']);
		$obj->setGiaovien($data['tbl_giaovien']);
		$obj->setLophoc($data['tbl_lophoc']);
		$obj->setKythi($data['tbl_kythi']);
		
		return $obj;
	}
	
	protected function _insert(Data_Model_DomainObjectAbstract $obj)
	{
		$db = $this->getDb();
		$data = array(
				
				'TenMaKhoaHoc' => $obj->getMakhoahoc(),
				'TenKhoaHoc' => $obj->getTenkhoahoc(),
				'MaLop' => $obj->getMalop(),
				'NgayKhaiGiang' => $obj->getNgaykhaigiang(),
				'MaGiaoVien' => $obj->getMagiaovien(),
				'TrangThai'  => $obj->getTrangthai()
				);
		$db->insert('tbl_khoahoc',$data);
	}
	
	protected function _update(Data_Model_DomainObjectAbstract $obj)
	{
		$db = $this->getDb();
		$data = array(
				'TenMaKhoaHoc' => $obj->getMakhoahoc(),
				'TenKhoaHoc' => $obj->getTenkhoahoc(),
				'MaLop' => $obj->getMalop(),
				'NgayKhaiGiang' => $obj->getNgaykhaigiang(),
				'MaGiaoVien' => $obj->getMagiaovien(),
				'TrangThai'  => $obj->getTrangthai()
		);
		$where = $db->quoteInto('MaKhoaHoc = ?', $obj->getId());
		$db->update('tbl_khoahoc',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaKhoaHoc = ?',$id);
		$db->delete('tbl_khoahoc',$where);
	}
	
	// Goi chinh no
	
	public static function getInstance()
	{
		if(null === self::$_instance)
		{
			self::$_instance = new self();
		}
		return self::$_instance;
	}
} 